package de.haw.shc.Config;

import de.haw.shc.Interfaces.DeviceProperty.Device;
import de.haw.shc.JSONObjects.Blinds;
import de.haw.shc.JSONObjects.Curtain;
import de.haw.shc.JSONObjects.Light;
import de.haw.shc.JSONObjects.Window;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: mizus
 * Date: 29.05.13
 * Time: 14:23
 * To change this template use File | Settings | File Templates.
 */

public class Device_Content_Handler extends DefaultHandler implements ContentHandler {

    private Map<String, List<Device>> device_Map = new HashMap<String, List<Device>>();
    private Map<String, List<Device>> room_Map = new HashMap<String, List<Device>>();

    private Device device;
    private String currentValue;

    @Override
    public void setDocumentLocator(Locator locator) {
        //To change body of implemented methods use File | Settings | File Templates.
    }


    @Override
    public void startDocument() throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }


    @Override
    public void endDocument() throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }



    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {


        if(localName.equals("Light")){
            device = new Light();
        }

        if(localName.equals("Window")){
            device = new Window();
        }

        if (localName.equals("Curtain"))
            device = new Curtain();

        if(localName.equals("Blinds"))
            device = new Blinds();

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if(localName.equals("viewname"))
            device.setViewName(currentValue);

        if (localName.equals("viewRaumName"))
            device.setViewRaumName(currentValue);

        if (localName.equals("name"))
            device.setName(currentValue);

        if (localName.equals("raum"))
            device.setRaum(currentValue);

        if (localName.equals("color"))
            device.setColorable(Boolean.valueOf(currentValue));


        if(localName.equals("Blinds") || localName.equals("Window")
                || localName.equals("Light") || localName.equals("Curtain")) {
            add(device);
            fill(device, device.getViewRaumName(), room_Map);
        }

    }



    private void add(Device aDevice){
        if (aDevice instanceof Light)         fill(aDevice, "Licht", device_Map);
        else if ( aDevice instanceof Window)  fill(aDevice, "Fenster", device_Map);
        else if ( aDevice instanceof  Blinds) fill(aDevice, "Rollos", device_Map);
        else if ( aDevice instanceof Curtain) fill(aDevice, "Gardine", device_Map);
    }

    private void fill(Device aDevice, String key, Map<String, List<Device>> valMap){
        if(valMap.containsKey(key)){
            List<Device> valList = valMap.get(key);
            valList.add(aDevice);
        } else{
            List<Device> valList = new ArrayList<Device>();
            valList.add(aDevice);
            valMap.put(key, valList);
        }
    }


    public Map<String, List<Device>> getMap_by_Device(){
        return device_Map;
    }

    public Map<String, List<Device>> getMap_by_Room(){
        return room_Map;
    }



    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        currentValue = new String(ch, start, length);
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void processingInstruction(String target, String data) throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void skippedEntity(String name) throws SAXException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
